public class Test {
    public static void main(String[] args) {
        //认识多态：对象多态、行为多态
        //多态的前提：有继承/实现关系   存在父类引用子类对象  存在重写方法
    People p1 = new Student();
    p1.run();//识别技巧：编译看左边,运行看右边
    System.out.println(p1.name);//对于变量,编译看左边,运行也看左边
    //强制类型转换
    Student s1 = (Student) p1;
    s1.test();
    //强制类型转换可能存在的问题：编译阶段有继承或者实现关系就可以强制转换,但是运行时可能出现类型转换错误
//    Teacher t1 = (Teacher) p1;    //ClassCastException

    //推荐语法
    if(p1 instanceof Student){
        Student s2 = (Student) p1;
        s1.test();
    }else {
        Teacher t2 = (Teacher) p1;
        t2.teach();
    }

    People p2 = new Teacher();
    p2.run();//识别技巧：编译看左边,运行看右边
    System.out.println(p2.name);//对于变量,编译看左边,运行也看左边
    }
}
